package com.hw

import org.apache.spark.{SparkConf, SparkContext}
import org.apache.spark.rdd.RDD

/**
 * 统计都及格的学生
 */
object Demo5DouJiGeStudents {
  def main(args: Array[String]): Unit = {

    val conf = new SparkConf()
    conf.setMaster("local")
    conf.setAppName("Filter")
    val sc = new SparkContext(conf)
    val subRDD: RDD[String] = sc.textFile("data/subject.txt")
    val scoreRDD: RDD[String] = sc.textFile("data/score.txt")
    val mapSub: RDD[(String, (String, String))] = subRDD.map(
      line => {
        val split: Array[String] = line.split(",")
        (split(0), (split(1), split(2)))
      }
    )
    val mapScore: RDD[(String, (String, String))] = scoreRDD.map(
      line => {
        val split: Array[String] = line.split(",")
        (split(1), (split(0), split(2)))
      }
    )
    val join: RDD[(String, ((String, String), (String, String)))] = mapSub.join(mapScore)
    val resRDD: RDD[(String, Iterable[(String, String, Int, Int)])] = join.map(
      kv => {
        (kv._2._2._1, kv._1, kv._2._2._2.toInt, kv._2._1._2.toInt)
      }
    ).filter(
      line => {
        line._3 >= line._4 * 0.6
      }
    ).groupBy(
      line => line._1
    ).filter(
      line => line._2.size == 6
    )
    resRDD.map {
      case (id: String, iter: Iterable[(String, String, Int, Int)]) =>
        val list: List[(String, String, Int, Int)] = iter.toList
        for (elem <- list) {
          println(elem)
        }
    }.foreach(println)


  }


}
